function [Data_Rf, acqLen] = DataExtractFromBin(bin_file_path, number_assigned_zero)

% 从bin文件提取信号
% 输入参数：
% bin_file_path  :  原始数据bin文件路径
% number_assigned_zero： 对每个通道采集的原始数据前XX个数据赋值为0
% 输出参数：
% Data_Rf   ： 采集到的超声和光声数据
% acqLen    :  每个通道采集的点数（对应成像深度）
% Example：
% [Data_Rf, acqLen] = DataExtractFromBin('E:\Projects\Test\Rawdata_FPGA1_Frame0.bin', 100)

filedir = fileparts(bin_file_path);

frameNum   = 6;                             %线数 LineNum   （6： PA=1frame US=5frames）
%%fileEndNum = 9;                             %bin文件end数   （end=0  第一个文件 ）
fileEndNum = 0;                             %只使用一个bin文件，暂时
allFileNum = fileEndNum + 1;                %bin文件的总数    （若10个bins：0...9）

channelNum = 64;                             %阵元数目固定64

acqLen=4000; 
% Data = zeros(acqLen,channelNum,frameNum,allFileNum);
US_RfData = zeros(acqLen,channelNum,frameNum,allFileNum);
PA_RfData = zeros(acqLen,channelNum,1,allFileNum);
for fileNum = 0:fileEndNum
    %生成文件名
    %fileName = sprintf("%s\\%s%d%s",filedir,'\Rawdata_FPGA1_Frame',fileNum,'.bin');
    fileName = bin_file_path;   %只使用一个文件，暂时
    fp = fopen(fileName, 'rb');
    hdr1 = fread(fp, 1, 'uint8');
    hdr2 = fread(fp, 1, 'uint8');
    hdr3 = fread(fp, 1, 'uint8');
    shotLen = hdr1 + hdr2 * 256 + hdr3 * 256 * 256;

   
    acqLen  = shotLen / 16; %(77mm deep 4000pionts)
    header = fread(fp, 5, 'uint8');
    
%acqlen    :数据
%channelNum:通道索引
%frameNum  :raw文件中帧数
%allFileNum:文件夹中所有bin文件数目(计算所有数据平均值用)
% Data = zeros(acqLen,channelNum,frameNum,allFileNum);

    for j=1:frameNum %
        frewind(fp);
        for i=1:j %读取某帧数据
            if(i>1)
              fread(fp, 8, 'uint8');
            end
            data = fread(fp, 64 * 2 * acqLen, 'uint8');
        end
        b2 = data(1:2:end); % to multiply 256
        b1 = data(2:2:end);
        
        data = b2 * 256 - 65536 * (b2 >= 128) + b1;
        
        data = reshape(data, 4, acqLen * 16);
        data = flipud(data);
        data = data(:);
        rf_data = transpose(reshape(data, channelNum, acqLen));
        if(j==1)
            Data_PA_Rf(:,:,1,fileNum+1) = rf_data;
        else
            tempData_US_Rf(:,:,j,fileNum+1) = rf_data;
        end

    end
fclose(fp);
end
%Data_US_Rf=tempData_US_Rf(1:2400,:,2:6,:);
Data_US_Rf=tempData_US_Rf(1:acqLen,:,2:6,:);


% Data_US_Rf = detrend(mean(mean(Data_US_Rf(1:2400,:,1:5,:),4),3));  % (60mm deeep 3200point 1500m/s)    %(45mm deeep 2400point in water 1500m/s)
% Data_PA_Rf = detrend(mean(Data_PA_Rf(1:2400,:,1,:),4));   %(60mm deeep 3200point in water 1500m/s)   %(45mm deeep 2400point in water 1500m/s)

Data_US_Rf = detrend(mean(mean(Data_US_Rf(1:acqLen,:,1:5,:),4),3));  % (60mm deeep 3200point 1500m/s)    %(45mm deeep 2400point in water 1500m/s)
Data_PA_Rf = detrend(mean(Data_PA_Rf(1:acqLen,:,1,:),4));   %(60mm deeep 3200point in water 1500m/s)   %(45mm deeep 2400point in water 1500m/s)


Data_Rf.US = Data_US_Rf;
Data_Rf.PA = Data_PA_Rf;

end